<template>
  <div>
    <div>
      <el-button type="success" size='small' @click="dialogVisible()" class="addClick">{{$t('添加')}}</el-button>
      <el-button type="danger" size='small' @click="deleteData" class="delClick">{{$t('删除')}}</el-button>
      <el-button type="danger" size='small' @click="clearData" class="clearData">{{$t('清空')}}</el-button>
    </div>
    <el-table size='mini' ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align='center' width="55">
      </el-table-column>
      <el-table-column :label="$t('IP地址')" align='center' min-width="300">
        <template slot-scope="scope">{{ scope.row.ip }}</template>
      </el-table-column>
      <el-table-column :label="$t('域名')" align='center' min-width="300">
        <template slot-scope="scope">{{ scope.row.domain }}</template>
      </el-table-column>
    </el-table>
    <hosts-dialog v-if="hostsDialogVisible" ref="refHostsDialogVisible"></hosts-dialog>
  </div>
</template>
<script>
import { getHostsArray, delHostsInfo, clearHosts } from '@/api/network/dnsHosts/hosts'
import HostsDialog from './components/HostsDialog'
export default {
  name: 'Hosts',
  components: {
    HostsDialog
  },
  data () {
    return {
      hostsDialogVisible: false,
      tableData: [],
      rule_map: []
    }
  },
  created () {
    this.listData()
  },
  methods: {
    // 多选按钮
    handleSelectionChange (data) {
      this.rule_map = data
    },
    // 列表
    listData () {
      getHostsArray().then(res => {
        if (res.status === 0) {
          this.tableData = res.info[0].hosts
        }
      })
    },
    // 添加按钮表单
    dialogVisible (data) {
      this.hostsDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refHostsDialogVisible.init(data)
      })
    },
    deleteData () {
      if (this.rule_map && this.rule_map.length > 0) {
        this.$confirm(this.$t('您是否要删除所有选中的数据？'), this.$t('提示'), {
          closeOnClickModal: false,
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          confirmButtonClass: 'confirmButton',
          cancelButtonClass: 'confirmCancelButton',
          type: 'warning'
        }).then(() => {
          delHostsInfo({ rule_map: JSON.stringify(this.rule_map) }).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('删除成功'),
                type: 'success'
              })
              this.rule_map = []
              this.listData()
            }
          })
        })
      } else {
        this.$messages.error(this.$t('请至少选择一条数据进行删除'))
      }
    },
    clearData () {
      this.$confirm(this.$t('您是否要清空所有数据？'), this.$t('提示'), {
        closeOnClickModal: false,
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        confirmButtonClass: 'confirmButton',
        cancelButtonClass: 'confirmCancelButton',
        type: 'warning'
      }).then(() => {
        clearHosts().then(res => {
          if (res.status === 0) {
            this.$message({
              message: this.$t('清空成功'),
              type: 'success'
            })
            this.listData()
          }
        })
      })
    }
  }
}
</script>
